//
// Created by none98 on 2021/9/10.
//

#ifndef LOGINSERVER_EVENTLOOP_H
#define LOGINSERVER_EVENTLOOP_H

#include<vector>
#include <memory>
#include <map>

class Selector;
class Channel;
class TimerEventQueue;
class Timer;
class EventLoop {
public:
    EventLoop();
    ~EventLoop() = default;

    Selector* GetSelector(){
        return selector.get();
    }

    inline void SetTimeout(int timeout){
        this->timeout=timeout;
    }
    void StartLoop();

    long AddTimer(const Timer &timer);
    void CancelTimerBySeq(long seq);
    void CancelTimerByfd(int fd);
    void AddTask(const std::function<void()> &task);
    void HandleTasks();
private:
    int timeout;
    std::unique_ptr<Selector> selector;
    std::unique_ptr<TimerEventQueue> timerQueue;
    std::vector<Channel*> activeChannels;

    std::mutex taskQueueMutex;
    std::vector<std::function<void()>> taskQueue;
};


#endif //LOGINSERVER_EVENTLOOP_H
